package it.tic.sbibm.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ValidationChecks {


   /**
  	* Essential formal checks to verifiy email address validity.
  	*
  	* @return false if uncorrect address
  	* @param mailAddress
  	*/
	public static boolean isValidMailAddress(String mailAddress) {
		//
		// assuming at least x@x.xx
		//
		if (mailAddress.length() < 6) return false;
		if (mailAddress.length() > 256) return false;
		int length = mailAddress.length();
		// seek position of @
		int atIndex = mailAddress.indexOf("@");
		// if no @ is present is error
		if (atIndex < 0) return false;
		// @ must not be first character
		if (atIndex == 0) return false;
		// @ must be at least 4 positions before the end
		if (atIndex > length - 5) return false;
		// if there's more than one @ is error
		if(atIndex != mailAddress.lastIndexOf("@")) return false;
		
		// look for position of the last point
		int dotIndex = mailAddress.lastIndexOf(".");
		// must be at least one point
		if (dotIndex < 0) return false;
		// the last point must be after @
		if (dotIndex < atIndex) return false;
		// @ must be at least 2 positions before the end
		if (dotIndex > length - 3) return false;

		return true;
	  }
	  
	  public static boolean isValidUserid(String userId) {
	
	    Pattern p = Pattern.compile("[a-zA-Z0-9\\-]+");
	    
	    Matcher m = p.matcher(userId);
	    
	    return m.matches();
	
	  }
	  
	public static boolean isEmpty(Object val) {
		if (val == null) return true;
		if (val instanceof String && val.toString().length() == 0) return true;
		return false;
	}
}
